<?php

class ArticlesController extends Zend_Controller_Action
{
    protected $model;
    protected $eventModel;

    protected function getModel() {
        if(is_null($this->model)) {
            require_once 'Article.php';
            $this->model = new Article();
        }

        return $this->model;
    }

    protected function getEventModel() {
        if(is_null($this->eventModel)) {
            require_once 'Event.php';
            $this->eventModel = new Event();
        }

        return $this->eventModel;
    }
    
    public function init()
    {
        $this->view->controleur="articles";
    }

    /* page d'accueil sur les articles
     * affiche la liste des articles publiés suivant l'ID du user
     * si utilisateur anonyme, aucun article affiché (voire page indisponible)
     */
    public function indexAction()
    {
        $this->view->articles = $this->getModel()->getLatestArticle(5);
    }

    /* Formulaire de rédaction d'un nouvel article
     */
    public function newAction()
    {
        $this->view->eventList = $this->getEventModel()->getAllEvents();
    }

    /* éditer un article dans les brouillons
     * user only
     */
    public function editAction()
    {
        $this->view->article = $this->getModel()->getArticle($this->_getParam("id"));
    }

    /* lire un article
     */
    public function viewAction()
    {
        $this->view->article = $this->getModel()->getArticle($this->_getParam("id"));
    }

    /* fonction de recherche sur les articles
     */
    public function searchAction()
    {
        $this->view->search = $this->_getParam("search");
        $this->view->articleList = $this->getModel()->searchArticle($this->_getParam("search"));
        $this->view->eventList = $this->getEventModel()->searchEvent($this->_getParam("search"));
    }

    /* publication d'un nouvel article
     */
    public function publishAction()
    {
        if($_POST['id_user'] == "null") {
            $_POST['id_user'] = NULL;
        }
        $this->getModel()->save($_POST);
    }

    public function vieweventAction()
    {
        $this->view->event = $this->getEventModel()->getEvent($this->_getParam("id"));
        $this->view->articleList = $this->getModel()->getArticleWhereEventIs($this->_getParam("id"));
    }
}

